package com.ypj.rpc.entity;

import com.ypj.rpc.enumeration.ResponseCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class RpcResponse<T> implements Serializable {

    /**
     * 响应对应的请求号
     */
    private String requestId;
    /**
     * 响应状态码
     */
    private Integer statusCode;
    /**
     * 响应状态补充信息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;

    /**
     * 生成响应成功对象
     */
    public static <T> RpcResponse<T> success(T data,String requestId){
        RpcResponse<T> response=new RpcResponse();
        response.setRequestId(requestId);
        response.setStatusCode(ResponseCode.SUCCESS.getCode());
        response.setData(data);
        return response;
    }
    /**
     * 生成响应失败对象
     */
    public static <T> RpcResponse<T> fail(ResponseCode code,String requestId){
        RpcResponse response=new RpcResponse();
        response.setRequestId(requestId);
        response.setStatusCode(code.getCode());
        response.setMessage(code.getMessage());
        return response;
    }
}
